package com.wo.cloud.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wo.cloud.admin.dto.SysUserNotifyDto;
import com.wo.cloud.admin.entity.SysNotify;
import com.wo.cloud.admin.entity.SysUserNotify;
import com.wo.cloud.admin.entity.User;
import com.wo.cloud.admin.mapper.SysNotifyMapper;
import com.wo.cloud.admin.mapper.SysUserNotifyMapper;
import com.wo.cloud.admin.service.ISysNotifyService;
import com.wo.cloud.core.entity.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/15 22:45
 * Description:
 */

@Service
public class SysNotifyServiceImpl extends ServiceImpl<SysNotifyMapper, SysNotify> implements ISysNotifyService {

    @Autowired
    private SysNotifyMapper sysNotifyMapper;

    @Autowired
    private SysUserNotifyMapper sysUserNotifyMapper;

    /**
     * 获取用户下所有系统通知消息
     */
    @Override
    public Response getAllSysNotify() {
        // 从 token 中解析用户

        // 获取用户创建时间，在通知发布之后的新用户是无法获取到通知的
        User user = new User();
        user.setId("11223344");
        user.setCreateTime(LocalDateTime.of(2021, 5, 10, 22, 52, 4));

        // 更新通知
        getNewNotify(user);

        // 查询用户下系统通知
        List<SysUserNotifyDto> sysUserNotifies = sysUserNotifyMapper.getAllSysNotify(user.getId());

        return Response.data(sysUserNotifies);
    }

    @Override
    public Response getUnreadNotify() {
        // 从 token 中解析用户

        // 获取用户创建时间，在通知发布之后的新用户是无法获取到通知的
        User user = new User();
        user.setId("11223344");
        user.setCreateTime(LocalDateTime.of(2021, 5, 10, 22, 52, 4));

        // 更新通知
        getNewNotify(user);

        // 查询用户下系统通知
        List<SysUserNotifyDto> sysUserNotifies = sysUserNotifyMapper.getUnreadNotify(user.getId());

        return Response.data(sysUserNotifies);
    }

    public IPage<SysNotify> getSysNotifyPage(Page page) {
        return sysUserNotifyMapper.selectPage(page, null);
    }

    /**
     * 关联新通知到用户
     *
     * @param user
     */
    private void getNewNotify(User user) {
        // 先从Notify中拉取消息
        QueryWrapper<SysNotify> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("create_time", user.getCreateTime());
        queryWrapper.eq("type", "1");
        List<SysNotify> sysNotifies = sysNotifyMapper.selectList(queryWrapper);

        List<String> sysNotifyIds = sysNotifies.stream().map(SysNotify::getId).collect(Collectors.toList());

        String userId = user.getId();
        // 查询 通知消息是否已经被关联到用户，每次更新只将新通知关联到用户
        QueryWrapper<SysUserNotify> sysUserNotifyQueryWrapper = new QueryWrapper<>();
        sysUserNotifyQueryWrapper.eq("user_id", userId);
        List<SysUserNotify> sysUserNotifies = sysUserNotifyMapper.selectList(sysUserNotifyQueryWrapper);

        // 获取 notifyId 集合
        List<String> sysUserNotifyIds = sysUserNotifies.stream().map(SysUserNotify::getNotifyId).collect(Collectors.toList());

        // 将 sysNotifies 集合中存在却不在 sysUserNotifies 集合中的新数据
        List<String> newNotifyIds = (List<String>) CollectionUtils.subtract(sysNotifyIds, sysUserNotifyIds);

        if (ObjectUtil.isNotNull(newNotifyIds)) {
            // 将消息关联到用户, 并将读取状态置为0
            for (String notifyId : newNotifyIds) {
                SysUserNotify sysUserNotify = new SysUserNotify();
                sysUserNotify.setNotifyId(notifyId);
                sysUserNotify.setIsRead(false);
                sysUserNotify.setUserId(userId);
                sysUserNotifyMapper.insert(sysUserNotify);
            }
        }
    }
}
